from django.shortcuts import render
from drf_yasg import openapi
from drf_yasg.utils import swagger_auto_schema
from rest_framework import status
from rest_framework.decorators import action
from rest_framework.permissions import IsAuthenticated
from rest_framework.response import Response
from rest_framework.viewsets import ModelViewSet

from .permissions import CampusOrganizationManagingPermission
from .serializers import *


# Create your views here.
class CampusOrganizationViewSet(ModelViewSet):
    queryset = CampusOrganizations.objects.all().filter(accept=True)
    serializer_class = CampusOrganizationsSerializer

    permission_classes = [CampusOrganizationManagingPermission]


class CampusOrganizationsManagerViewSet(ModelViewSet):
    queryset = CampusOrganizationsManager.objects.all().filter(accept=True)
    serializer_class = CampusOrganizationsManagerSerializer
    permission_classes = [IsAuthenticated]


class CampusOrganizationActivityViewSet(ModelViewSet):
    queryset = CampusOrganizationActivity.objects.all()
    serializer_class = CampusOrganizationActivitySerializer
    permission_classes = [CampusOrganizationManagingPermission]

    @swagger_auto_schema(method='post', request_body=openapi.Schema(
        type=openapi.TYPE_OBJECT,
        properties={
            'timelines': openapi.Schema(
                type=openapi.TYPE_ARRAY,
                items=openapi.Schema(
                    type=openapi.TYPE_OBJECT,
                    properties={
                        'activity': openapi.Schema(type=openapi.TYPE_NUMBER, description='活动id'),
                        'time': openapi.Schema(type=openapi.FORMAT_DATETIME, description="ISO时间"),
                        'todo': openapi.Schema(type=openapi.TYPE_STRING, description='干什么事情')
                    }
                ))
        }
    ))
    @action(methods=['POST'], detail=True,
            permission_classes=[CampusOrganizationManagingPermission])
    def add_timeline(self, request, pk):
        # print(request.data)
        data = CampusOrganizationActivitySerializer(data=request.data.timelines, many=True)
        data.is_valid()
        print(data.validated_data)
        data.save()
        return Response(data={
            "success": True,
            "code": 200,
            "msg": "成功添加",
        }, status=status.HTTP_200_OK)
        # print(data)
